package com.palnar.ilecker.manager;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import com.palnar.ilecker.constants.IServiceConstants;
import com.palnar.ilecker.utils.Utils;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class ServiceManager extends AsyncTask<Void, Void, SoapObject> implements
		IServiceConstants {
	DataDownloadListener dataDownloadListener;

	private String response, wsdlString;
	private String[] name;// , value;

	private ArrayList<Object> value;

	private String soapActionForFunction;
	private String method;
	private Context context;
	private CustomProgressDialog progress;

	private SoapObject soapObject;
	private SoapObject soapObjectResponse;

	private boolean isException = false;
	private String exceptionMsg = "";

	public ServiceManager(String wsdlString, String[] name,
			ArrayList<Object> value, String method,
			String soapActionForFunction, CustomProgressDialog progress) {
		this.wsdlString = wsdlString;
		this.name = name;
		this.value = value;
		this.method = method;
		this.soapActionForFunction = soapActionForFunction;
		this.progress = progress;

		Log.e("TEST", "**********START*************");
		Log.e("TEST", " URL= " + wsdlString);
		Log.e("TEST", " METHOD= " + method);
		Log.e("TEST", " ACTION= " + soapActionForFunction);
		Log.e("TEST", "***********END************");

	}

	public void setServiceListener(DataDownloadListener dataDownloadListener) {
		this.dataDownloadListener = dataDownloadListener;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		 progress.show();

		soapObject = new SoapObject(NAMESPACE, method);

		for (int i = 0; i < value.size(); i++) {

			PropertyInfo pi = new PropertyInfo();

			pi.setName(name[i]);
			pi.setValue(value.get(i));

			if (value.get(i) instanceof String) {
				pi.setType(PropertyInfo.STRING_CLASS);
			} else if (value.get(i) instanceof Boolean) {
				pi.setType(PropertyInfo.BOOLEAN_CLASS);
			}

			soapObject.addProperty(pi);

		}

		System.out.println("PRE...");

	}

	@Override
	protected SoapObject doInBackground(Void... params) {

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.dotNet = true;

		envelope.setOutputSoapObject(soapObject);

		AndroidHttpTransport httpTransport = new AndroidHttpTransport(
				wsdlString);

		httpTransport.debug = true;

		try {
			System.setProperty("http.keepAlive", "false");

			httpTransport.call(soapActionForFunction, envelope);

		}
		catch (Exception e) {
			isException = true;
			exceptionMsg = e.getMessage();

		}

		try {

			soapObjectResponse = (SoapObject) envelope.getResponse();
		} catch (SoapFault e) {
			e.printStackTrace();

			isException = true;
			exceptionMsg = e.getMessage();
		} catch (Exception e) {
			isException = true;
			exceptionMsg = e.getMessage();

		}

		return soapObjectResponse;
	}

	@Override
	protected void onPostExecute(SoapObject result) {
		super.onPostExecute(result);
		 progress.dismiss();

		System.out.println("POST...");
		if (isException) {

		
			dataDownloadListener.dataDownloadedFailed(exceptionMsg);
		} else {
			dataDownloadListener.dataDownloadedSuccessfully(result);
			
			Utils.longInfo(result.toString());
			
		}

	}

	public static interface DataDownloadListener {

		void dataDownloadedSuccessfully(SoapObject result);

		void dataDownloadedFailed(String error);

	}

}
